package com.zhongjianjian.elasticjob.taskCpnfig;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.zhongjianjian.elasticjob.MyJobTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @类名: com.zhongjianjian.elasticjob.taskCpnfig
 * @作者: lgt
 * @创建时间: 2020-03-01 16:15
 * @描述:
 **/
@Configuration
public class MyElasticJobConfig {
    @Autowired
    private ZookeeperConfig zookeeperConfig;

    @Bean("jobCoreConfiguration")
    public JobCoreConfiguration getJobConfigure(){
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("Test-Job", "0/10 * * * * ?", 4)
                .shardingItemParameters("0=T0,1=T1,2=T2,3=T3")//分片的参数配置
                .jobParameter("JOb的参数")
                .failover(true)//允许失效转移
                .description("Job的描述，介绍这个JOB是干什么的")
                .misfire(false)
                .build();
        return jobCoreConfiguration;
    }

    @Bean("simpleJobConfig")
    public SimpleJobConfiguration getsimpleJobType(JobCoreConfiguration jobCoreConfiguration){
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(jobCoreConfiguration, MyJobTask.class.getCanonicalName());

        return simpleJobConfig;
    }

    @Bean(value = "liteJobConfiguration")
    public LiteJobConfiguration getLiteJobConfigure(SimpleJobConfiguration simpleJobConfig){
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return liteJobConfiguration;
    }

    /**
     * 配置Zookeeper 信息
     * @param zookeeperConfig
     * @return
     */
    @Bean("zookeeperConfiguration")
    public ZookeeperConfiguration getZookeeperConfiguration(ZookeeperConfig zookeeperConfig){

        ZookeeperConfiguration zookeeperConfiguration= new ZookeeperConfiguration(zookeeperConfig.getLists(),zookeeperConfig.getNamespace());

        return zookeeperConfiguration;
    }

    /**
     * 配置zookeepr注册中心
     * @param zookeeperConfiguration
     * @return
     */
    @Bean(value = "zookeeperRegistryCenter",initMethod = "init")
    public ZookeeperRegistryCenter getZookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        return zookeeperRegistryCenter;
    }

    /**
     * 配置JOb的启动
     * @param liteJobConfiguration
     * @param zookeeperRegistryCenter
     * @return
     */
    @Bean(initMethod = "init")
    public JobScheduler getJobScheduler(LiteJobConfiguration liteJobConfiguration, ZookeeperRegistryCenter zookeeperRegistryCenter){
        return new JobScheduler(zookeeperRegistryCenter, liteJobConfiguration);
    }
}
